/*
 *  Copyright (C) 2010 Francesco Boffa (aka AlfaOmega08)
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package Engine;

import java.io.*;
import java.util.*;

public class Config {
	static Hashtable<String, String> ConfArray = new Hashtable<String, String>();

	public static void Init() {
		ConfArray.put("LongLanguage", "Italiano");
		ConfArray.put("Language", "it");
		ConfArray.put("Country", "IT");

		ConfArray.put("BiosPath", "SCHP1001.bin");
	}

	public static void setConfig(String k, String v) {
		ConfArray.put(k, v);
	}

	public static String getConfig(String k) {
		return ConfArray.get(k);
	}

	public static void removeConfig(String k) {
		ConfArray.remove(k);
	}

	public static void Save() {
		try {
			File file = new File("pps1e.cfg");
			BufferedWriter output = new BufferedWriter(new FileWriter(file));

			Set<String> set = ConfArray.keySet();
			Iterator<String> itr = set.iterator();
			while (itr.hasNext()) {
				String curKey = itr.next();
				String curValue = ConfArray.get(curKey);

				output.write(curKey + "=" + curValue);
				output.newLine();
			}

			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void AddRecent(String Path) {
		for (int i = 0; i < 5; i++) {
			String cur = getConfig("Recent" + i);
			if (cur != null) {
				if (cur.equals(Path)) {
					removeConfig("Recent" + i);
					// TODO: scale all subsequent elements up;
				}
			}
		}

		/* Scroll all elements down */
		for (int i = 3; i >= 0; i--) {
			String cur = getConfig("Recent" + i);
			if (cur != null)
				setConfig("Recent" + (i + 1), cur);
		}

		/* Set the opening image as the first (most) recent image */
		setConfig("Recent0", Path);
	}
}
